package com.example.demo.controller;

import com.example.demo.entity.Daka;
import com.example.demo.entity.Login;
import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/std")
public class HomeController {

    @Autowired
    private StudentService studentService;

//    @RequestMapping("/home")
//    public String home() {
//        List<Student> list = studentService.Sel2();
//        System.out.println(list);
//        return "home";
//    }
    /**
     * 查询出已经打卡的全部信息，返回
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/findAll/{page}/{size}")
    public List<Student> findAll(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        List<Student> list = studentService.Sel3();
        List<Student> ns = null;
        if(list.size()>=(page*size)){
            ns =  list.subList((page-1)*size, page*size);
        }else{
            ns = list.subList((page-1)*size, list.size());
        }
        return ns;
    }

    @ResponseBody
    @GetMapping("/findNum")
    public int findNum(){
        List<Student> list = studentService.Sel3();
        return list.size();
    }

    /**
     * 查询出已经打卡的全部信息，返回
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/findAllClockedIn/{page}/{size}")
    public List<Student> findAllClockedIn(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        List<Student> list = studentService.Sel2();
        List<Student> ns = null;
        if(list.size()>=(page*size)){
            ns =  list.subList((page-1)*size, page*size);
        }else{
            ns = list.subList((page-1)*size, list.size());
        }
        return ns;
    }

    /**
     * 查询出未打卡的全部信息，返回
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/findAllNotClockedIn/{page}/{size}")
    public List<Student> findAllNotClockedIn(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        List<Student> list = studentService.Sel4();
        List<Student> ns = null;
        if(list.size()>=(page*size)){
            ns = list.subList((page-1)*size, page*size);
        }else{
            ns = list.subList((page-1)*size, list.size());
        }
        return ns;
    }

    @ResponseBody
    @GetMapping("/findStudent/{sid}")
    public List<Student> findStudent(@PathVariable("sid") String sid){
        List<Student> list = studentService.Sel5(Integer.valueOf(sid));
        return list;
    }

    @ResponseBody
    @GetMapping("/findClockedIn")
    public int findClockedIn(){
        List<Student> list = studentService.Sel2();
        return list.size();
    }

    @ResponseBody
    @GetMapping("/findNotClockedIn")
    public int findNotClockedIn(){
        List<Student> list = studentService.Sel4();
        return list.size();
    }

    @ResponseBody
    @PostMapping("/save")
    public String save(@RequestBody Student student){
        int i = studentService.insertStudent(student);
        if(i==1){
            return "success";
        }else {
            return "fail";
        }
    }

    @ResponseBody
    @GetMapping("/login/{username}/{password}")
    public Login login(@PathVariable("username") String username,@PathVariable("password") String password){
        Login login = new Login();
        login.setUsername(username);
        login.setPassword(password);
        Login i = studentService.login(login);
        if(i==null){
            return new Login();
        }else {
            return i;
        }
    }

    @ResponseBody
    @PostMapping("/updateState")
    public String updateState(@RequestBody Student student){
        int i = studentService.updateState(student);
        if(i==1){
            return "success";
        }else {
            return "fail";
        }
    }

    @ResponseBody
    @PostMapping("/updateStudent")
    public String updateStudent(@RequestBody Student student){
        int i = studentService.updateStudent(student);
        int j = studentService.updateLogin(student);
        if(i==1 && j==1){
            return "success";
        }else {
            return "fail";
        }
    }

    @ResponseBody
    @GetMapping("/release")
    public String release(){
        List<Student> list = studentService.Sel4();
        List<Student> list1 = studentService.Sel2();
        if(list.size()>0 || list1.size()>0){
            System.out.println("fail");
            return "fail";
        }else{
            System.out.println("success");
            List<Student> list2 = studentService.Sel3();
            studentService.insertDaka(list2);
            return "success";
        }
    }

    @ResponseBody
    @GetMapping("/deleteStudent/{sid}")
    public String deleteStudent(@PathVariable("sid") Integer sid){
        int i = studentService.deleteStudent(sid);
        System.out.println(i);
        if(i==1){
            return "success";
        }else{
            return "fail";
        }
    }

    @ResponseBody
    @GetMapping("/studentTodayDakaState/{id}")
    public int studentTodayDakaState(@PathVariable("id") Integer id){
        Student student = studentService.Sel6(id);
        System.out.println(student.getState());
        return student.getState();
    }

    @ResponseBody
    @GetMapping("/studentTodayDaka/{id}")
    public Student studentTodayDaka(@PathVariable("id") Integer id){
        Student ss = studentService.Sel8(id);
        System.out.println(ss);
        if(ss.getState()==1){
            return ss;
        }else{
            return new Student();
        }
    }

    /**
     * 根据sid,查询出历史打卡记录
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/studentDaka/{page}/{size}/{sid}")
    public List<Daka> studentDaka(@PathVariable("page") Integer page,@PathVariable("size") Integer size,@PathVariable("sid") Integer sid){
        List<Daka> list = studentService.Sel7(sid);
        List<Daka> dk = null;
        if(list.size()>=(page*size)){
            dk = list.subList((page-1)*size, page*size);
        }else{
            dk = list.subList((page-1)*size, list.size());
        }
        return dk;
    }

    @ResponseBody
    @GetMapping("/studentDakaNum/{sid}")
    public int studentDakaNum(@PathVariable("sid") Integer sid){
        List<Daka> list = studentService.Sel7(sid);
        return list.size();
    }

    @ResponseBody
    @PostMapping("/insertInformation")
    public int insertInformation(@RequestBody Student student){
        System.out.println(student);
        int i = studentService.insertInformation(student);
        return i;
    }

    @ResponseBody
    @PostMapping("/updateInformation")
    public int updateInformation(@RequestBody Student student){
        System.out.println(student);
        int i = studentService.updateInformation(student);
        return i;
    }

//    @RequestMapping("/hh")
//    public String hh(String stdId,Model model) {
//        List<String> l = new ArrayList<String>();
//        model.addAttribute("id",stdId);
//        model.addAttribute("subject",l);
//        int i = Integer.parseInt(stdId);
//        Student student = studentService.Sel(i);
//        model.addAttribute("std",student);
//        return "information";
//    }
//
//    @GetMapping("haha")
//    public String showDesignForm(String id,Model model) {
//        model.addAttribute("id",id);
//        model.addAttribute("name","haha.name");
//        return "haha";
//    }
//
//    @GetMapping("information")
//    public String information(Model model) {
//        List<String> l = new ArrayList<String>();
//        model.addAttribute("subject",l);
//        return "information";
//    }
//
//    @RequestMapping("/regist")
//    public String login(String[] subject,String id,Model model){
//        int i = Integer.parseInt(id);
//        Student student = studentService.Sel(i);
//        model.addAttribute("std",student);
//        List<String> l = new ArrayList<String>();
//        for(int k=0;k<subject.length;k++){
//            l.add(subject[k]);
//        }
//        model.addAttribute("subject",l);
//        return "information2";
//    }
}